package controller

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"ucenter/app/model"
	"ucenter/app/service"
	"ucenter/app/validator"
)

func Index(c *gin.Context) {
	c.JSON(200, &map[string]string{"adf": "222"})
}

func Login(c *gin.Context) {
	var (
		userVtr     validator.LoginValidator
		user        *model.User
		resKey      string
		returnData  map[string]interface{}
		userService *service.UserService
	)
	if err := c.ShouldBind(&userVtr); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}
	if vs, msg := userVtr.Valid(); !vs {
		c.JSON(http.StatusBadRequest, gin.H{"error": msg})
		return
	}
	returnData = make(map[string]interface{})
	userService = service.GetUserService()

	switch userVtr.LoginType {
	case validator.TOURIST:
		user, resKey = userService.TouristLogin(userVtr)
	case validator.GOOGLE, validator.FACEBOOK:
		oauthUser, err := service.GetThirdLoginInfoService().FetchUserInfoByToken(userVtr.LoginType, userVtr.IdToken)
		if err != nil {
			returnData["error"] = err.Error()
			returnData["code"] = "500"
			c.JSON(500, returnData)
			return
		}
		if oauthUser.Id != userVtr.GoogleId && userVtr.LoginType == validator.GOOGLE {
			returnData["error"] = "ID_TOKEN_NOT_MATCH"
			returnData["code"] = "404"
			c.JSON(400, returnData)
			return
		}
		if oauthUser.Id != userVtr.FacebookId && userVtr.LoginType == validator.FACEBOOK {
			returnData["error"] = "ID_TOKEN_NOT_MATCH"
			returnData["code"] = "404"
			c.JSON(400, returnData)
			return
		}
		user, resKey = userService.Login(userVtr)
	default:
		user, resKey = userService.Login(userVtr)
	}
	if resKey == "CODE_WRONG" {
		returnData["error"] = resKey
		returnData["code"] = "401"
		c.JSON(400, returnData)
		return
	}
	if resKey == "NEED_BIND_PHONE" {
		returnData["error"] = resKey
		returnData["code"] = "402"
		c.JSON(400, returnData)
		return
	}
	if resKey == "NO_REGISTER" {
		returnData["error"] = resKey
		returnData["code"] = "403"
		c.JSON(400, returnData)
		return
	}
	if resKey != "" {
		returnData["error"] = resKey
		returnData["code"] = "500"
		c.JSON(500, returnData)
		return
	}
	if rummy, err := service.Svc.Connector(); err == nil {
		returnData["rummy_node"] = map[string]interface{}{
			"host": rummy.Host,
			"port": rummy.Port,
		}
	}
	returnData["uid"] = user.Id
	returnData["token"] = user.Token
	returnData["code"] = "200"
	c.JSON(200, returnData)
}

/**
游客绑定
*/
func Bind(c *gin.Context) {
	var bindVal validator.BindValidator
	if err := c.ShouldBind(&bindVal); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}
	if vs, msg := bindVal.Valid(); !vs {
		c.JSON(http.StatusBadRequest, gin.H{"error": msg})
		return
	}
	returnData := make(map[string]string)
	userService := service.GetUserService()
	_, key := userService.TouristBind(bindVal)
	if key == "NO_REGISTER" {
		returnData["error"] = key
		returnData["code"] = "403"
		c.JSON(400, returnData)
		return
	}
	if key == "PHONE_EXIST" {
		returnData["error"] = key
		returnData["code"] = "404"
		c.JSON(400, returnData)
		return
	}
	if key != "" {
		returnData["error"] = key
		returnData["code"] = "500"
		c.JSON(500, returnData)
		return
	}
	returnData["code"] = "200"
	returnData["msg"] = "success"
	c.JSON(200, returnData)
}

/**
短信注册，如果有需要
*/
func Register(c *gin.Context) {
	var userVtr validator.LoginValidator
	if err := c.ShouldBind(&userVtr); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}
	if vs, msg := userVtr.Valid(); !vs {
		c.JSON(http.StatusBadRequest, gin.H{"error": msg})
		return
	}

	//userService := service.GetUserService()
	//user := new(model.User)
	//user.DeviceId = userVtr.DeviceId
	//userService.RegisterUser(user)
	c.JSON(200, &map[string]string{"adf": "222"})
}

/**
发送短信验证码
*/
func SendSMSCode(c *gin.Context) {
	phone := c.PostForm("phone")
	if phone == "" {
		c.JSON(200, &map[string]string{"error": "phone number is empty", "code": "400"})
		return
	}
	userService := service.GetUserService()
	err := userService.SendLoginCode(phone)
	if err != nil {
		c.JSON(200, &map[string]string{"error": "send error", "code": "401"})
		return
	}
	c.JSON(200, &map[string]string{"msg": "success", "code": "200"})
	return
}

func RummyNode(c *gin.Context) {
	node, err := service.Svc.Connector()
	if err != nil {
		c.JSON(500, &map[string]string{"error": "get node fail", "code": "500"})
		return
	}
	c.JSON(200, &map[string]interface{}{"node": node, "code": "200"})
	return
}
